#ifndef __CAUDIOIC_CFIFO_PLAY_BUFFER_H__
#define __CAUDIOIC_CFIFO_PLAY_BUFFER_H__

//65536+5K
#define FIFO_SINGLE_BUFFER_SIZE 0x10000
//5K
#define FIFO_PROTECT_BUFFER_SIZE 0x1400
//FIFO_SINGLE_BUFFER_SIZE - FIFO_PROTECT_BUFFER_SIZE => 0xEC00
#define FIFO_INPUT_BUFFER_SIZE 0xEC00

//#define ROUND_FILE_OFFSET(x) ((x)&~(unsigned long)(FIFO_SINGLE_BUFFER_SIZE-1))

//#define CURRENT_BUFFER_OFFSET(x) ((x)&(FIFO_SINGLE_BUFFER_SIZE-1))

#define BUFFER_NUMBER_TO_TAG(x) ((x)&0x1)

#define CHANGE_BUFFER_SIGNALED 0x2


typedef struct Mp3Buffer{
    Byte *Buffer;
    int ReadWriteTag;//0:CanWrite; 1:CanNotWrite;
}Mp3WRBuffer;

class CFIFOPlayBuffer
{
public:
    CFIFOPlayBuffer();
    ECode StartWriteThread();
    ECode StopWriteThread();
    Byte *GetAddr(Uint32 uCurrentOffset);
    ECode WriteThreadFunc();
public:
    Mp3WRBuffer m_WRBuffer[2];
private:
    IThread *m_pIWThread;
    int m_WriteThreadRun;
    DzEvent m_WriteEvent;
    int m_iChangedBufferSignal;
    int m_iCurrentReadBufferNO;
    int m_iCurrentWriteBufferNO;
    Uint32 m_uLastOffset;
    Byte * m_pCurrentPlayAddr;
    Byte *m_pStart;
    Byte *m_pProtect;
    Byte *m_pEnd;

private:
    void ResetAll();
    ECode FillPlayBuffer(Mp3WRBuffer *pWRBuffer, int* pReadedSize);
    ECode NewFillPlayBuffer(Uint32 uFileOffset);
    void GetNormalPlayAddr(Uint32 uCurrentOffset);
    void ChangePlayOffset(Uint32 uCurrentOffset);
    ECode ChangePlayBuffer();
    void SetFlagPoint(int Mp3WRBufferNO);
};

#endif //__CAUDIOIC_CFIFO_PLAY_BUFFER_H__

